package mx.com.initzero.ecommerce.dao.impl;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;

import mx.com.initzero.ecommerce.dao.CorreoDao;
import mx.com.initzero.ecommerce.dto.Correo;

public class CorreoDaoImpl extends JdbcDaoSupport implements CorreoDao{

	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

	@Autowired
	public CorreoDaoImpl(DataSource dataSource) {
		namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
		setDataSource(dataSource);
		JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource); 
	}
	
	@Override
	public int insertCorreo(Correo correo) {
		// TODO Auto-generated method stub
		final String query="insert into correo(idCorreo, correo) values(?,?)";
		System.out.println("intento insert correo");
		
		SimpleJdbcInsert insert=new SimpleJdbcInsert(getDataSource());
		insert.withTableName("correo");
		insert.usingGeneratedKeyColumns("idCorreo");
		
		Map<String, Object> parameters = new HashMap<String, Object>(2);
	    parameters.put("idCorreo",correo.getIdCorreo() );
	    parameters.put("correo",correo.getCorreo() );
	    
	    Number idcorreo = insert.executeAndReturnKey(parameters);
		return idcorreo.intValue();
	}

}
